package com.library.admin;

import javax.annotation.PostConstruct;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.library.admin.listener.LoginLogoutListener;
import com.library.admin.listener.UpdateMainPanelContentListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.VerticalLayout;

@Component
@Scope("session")
public class Menu extends VerticalLayout {
	private static final long serialVersionUID = -3202529927533816128L;
	
	private Button connectButton;
	private Button updateOwnAccountButton;
	private Button manageBooksButton;
	private Button manageUsersButton;
	private Button manageRentsButton;

	@PostConstruct
	public void init() {
		setWidth("100%");
		setImmediate(true);
		setSpacing(true);
		setMargin(true, false, false, true);
		
		connectButton = new Button("Se connecter");
		connectButton.setWidth("80%");
		
		connectButton.addListener(new LoginLogoutListener(this));
		
		addComponent(connectButton);
	}
	
	public void setSuperAdminMode() {
		connectButton.setCaption("Se déconnecter");
		connectButton.setEnabled(true);
		
		updateOwnAccountButton = new Button("Modifier vos données");
		manageBooksButton = new Button("Gestion des livres");
		manageUsersButton = new Button("Gestion des utilisateurs");
		manageRentsButton = new Button("Gestion des locations");
		
		updateOwnAccountButton.setWidth("80%");
		manageBooksButton.setWidth("80%");
		manageUsersButton.setWidth("80%");
		manageRentsButton.setWidth("80%");
		
		updateOwnAccountButton.addListener(new UpdateMainPanelContentListener());
		manageBooksButton.addListener(new UpdateMainPanelContentListener());
		manageUsersButton.addListener(new UpdateMainPanelContentListener());
		manageRentsButton.addListener(new UpdateMainPanelContentListener());
		
		addComponent(updateOwnAccountButton);
		addComponent(manageBooksButton);
		addComponent(manageUsersButton);
		addComponent(manageRentsButton);
	}
	
	public void setBookAdminMode() {
		connectButton.setCaption("Se déconnecter");
		connectButton.setEnabled(true);
		
		updateOwnAccountButton = new Button("Modifier vos données");
		manageBooksButton = new Button("Gestion des livres");
		
		updateOwnAccountButton.setWidth("80%");
		manageBooksButton.setWidth("80%");
		
		updateOwnAccountButton.addListener(new UpdateMainPanelContentListener());
		manageBooksButton.addListener(new UpdateMainPanelContentListener());
		
		addComponent(updateOwnAccountButton);
		addComponent(manageBooksButton);
	}
	
	public void setSubRentAdminMode() {
		connectButton.setCaption("Se déconnecter");
		connectButton.setEnabled(true);
		
		updateOwnAccountButton = new Button("Modifier vos données");
		manageUsersButton = new Button("Gestion des utilisateurs");
		manageRentsButton = new Button("Gestion des locations");
		
		updateOwnAccountButton.setWidth("80%");
		manageUsersButton.setWidth("80%");
		manageRentsButton.setWidth("80%");
		
		updateOwnAccountButton.addListener(new UpdateMainPanelContentListener());
		manageUsersButton.addListener(new UpdateMainPanelContentListener());
		manageRentsButton.addListener(new UpdateMainPanelContentListener());
		
		addComponent(updateOwnAccountButton);
		addComponent(manageUsersButton);
		addComponent(manageRentsButton);
	}
	
	public void setDisconnectedMode() {
		connectButton.setCaption("Se connecter");
		
		removeComponent(updateOwnAccountButton);
		removeComponent(manageBooksButton);
		removeComponent(manageUsersButton);
		removeComponent(manageRentsButton);
		
		//S'il existe des formulaires dans le menu, on les supprime
		for (int i = 0; i < getComponentCount(); i++) {
			com.vaadin.ui.Component currentComponent = getComponent(i);
			if (currentComponent instanceof Form) {
				removeComponent(currentComponent);
			}
		}
		AdmLibrary libraryApplication = (AdmLibrary) getApplication();
		libraryApplication.getMainPanel().removeAllComponents();
	}

	public Button getConnectButton() {
		return connectButton;
	}

	public Button getUpdateOwnAccountButton() {
		return updateOwnAccountButton;
	}

	public Button getManageBooksButton() {
		return manageBooksButton;
	}

	public Button getManageUsersButton() {
		return manageUsersButton;
	}

	public Button getManageRentsButton() {
		return manageRentsButton;
	}
}
